﻿#region references
using System;
using System.Configuration;
using System.Data.SqlClient;

/* Project dependencies */
using IcaGames.Logging;
#endregion

namespace IcaGames.Database
{
    /*
     * Class that holds common methods used to connect to a database.
     * 
     * @author
     * Rick Oonk
     * 
     * @version
     * 1.0
     * 
     * @update
     * 20-11-2009 by Rick Oonk.
     * Created Class and added attributes and standard functions. 
     */
    public static class Connection
    {
        #region connString
        private static string connString = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
        #endregion

        /*
         * Function open a new SQL Connection given a specified ConnectionString.
         * 
         * @returns
         * An opened SQL Connection on success.
         */ 
        public static SqlConnection Connect()
        {
            SqlConnection conn = new SqlConnection(connString);
            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while setting up new SqlConnection", "CONNECTION");
                Log.CreateLog(ex.Message, "CONNECTION");
            }

            return conn;
        }

        /*
         * Function to get a new SqlCommand with an opened SqlConnection.
         * 
         * @param
         * query: The query to use with the new SqlCommand.
         * 
         * @returns
         * A new SqlCommand using the "defaultConnection" connString.
         */
        public static SqlCommand GetCommand(string query)
        {
            SqlCommand command = new SqlCommand(query, Connect());
            return command;
        }

        /*
         * Function to close an opened SqlCommand.
         * 
         * @param
         * command: The opened SqlCommand that has to be closed.
         * 
         */
        public static void CloseCommand(SqlCommand command)
        {
            try
            {
                command.Connection.Close();
            }
            catch (Exception ex)
            {
                Log.CreateLog("SqlException while closing SqlCommand(1).", "CONNECTION");
                Log.CreateLog(ex.Message, "CONNECTION");
            }
        }

        /*
         * Function to close an opened SqlCommand and an opened SqlDataReader.
         * 
         * @param
         * command: The opened SqlCommand that has to be closed.
         * dr: The SqlDataReader that has to be closed.
         * 
         */
        public static void CloseCommand(SqlCommand command, SqlDataReader dr)
        {
            try
            {
                dr.Close();
                command.Connection.Close();
            }
            catch (Exception ex)
            {
                Log.CreateLog("SqlException while closing SqlCommand(2).", "CONNECTION");
                Log.CreateLog(ex.Message, "CONNECTION");
            }
        }

        /*
         * Function to get a new SqlDataReader using the specified SqlCommand.
         * 
         * @param
         * command: The SqlCommand that has to be used.
         * 
         * @returns
         * A new opened SqlDataReader using the specified SqlCommand.
         */
        public static SqlDataReader GetSqlDataReader(SqlCommand command)
        {
            SqlDataReader dr = null;
            try
            {
                dr = command.ExecuteReader();
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while executing GetSqlDataReader function.", "CONNECTION");
                Log.CreateLog(ex.Message, "CONNECTION");
            }
            return dr;
        }
    }
}
